﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;

namespace BLL
{
    public class ProjectMainManager
    {
        
        private ProjectMainService objProjectMainService = new ProjectMainService();
        #region 新增更改
        /// <summary>
        /// 添加或修改数据到项目主表
        /// </summary>
        /// <param name="PJMain"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string InsertOrUpdateProjectInfo(ProjectMain PJMain)
        {
            if(PJMain.PJId.Length>0)
            {
                objProjectMainService.UpdateProjectInfo(PJMain);
            }
            else
            {
                PJMain.PJId=GetNewNumber();
                objProjectMainService.InsertProjectInfo(PJMain);
            }
            return PJMain.PJId;
        }    
        /// <summary>
        /// 根据项目号更改项目主表状态
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="afterStatues"></param>
        /// <returns></returns>
        public int UpdateProjectInfo(int workParentId, string afterStatues)
        {
            return objProjectMainService.UpdateProjectInfo(workParentId, afterStatues);
        }
        /// <summary>
        /// 根据工作流号查询主表信息
        /// </summary>
        /// <param name="pjId"></param>
        /// <returns></returns>
        public ProjectMain GetProjectInfo(int workParentId)
        {
            return objProjectMainService.GetProjectInfo(workParentId);
        }
        #endregion
         /// <summary>
        /// 获取最大Id行的Number值,根据规则生成新Number值
        /// </summary>
        /// <returns></returns>
        public string GetNewNumber()
        {
            string result = "";
            string number = objProjectMainService.GetProjectLastRow();
            int year = Convert.ToInt32(number.Substring(2, 2));
            int month = Convert.ToInt32(number.Substring(4, 2));
            int log = Convert.ToInt32(number.Substring(6, 3));
            if (year == Convert.ToInt32(Convert.ToString(DateTime.Now.Year).Substring(2, 2)))
            {

                if (month == Convert.ToInt32(DateTime.Now.Month))
                {
                    log = log + 1;
                    result = "PJ" + Convert.ToString(year) + Convert.ToString(month).PadLeft(2, '0') + log.ToString().PadLeft(3, '0');
                }
                else
                {
                    result = "PJ" + Convert.ToString(year) + Convert.ToString(month + 1).PadLeft(2, '0') + "001";
                }
            }
            else
            {
                result = "PJ" + Convert.ToString(year + 1) + "01" + "001";
            }
            return result;
        }
    }
}
